package com.happy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.happy.dao.QuestionMapper;
import com.happy.entity.QuestionEntity;
import com.happy.service.IQuestionService;
import com.happy.util.StateEnum;
import com.happy.util.StateException;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, QuestionEntity> implements IQuestionService {

    @Resource
    private QuestionMapper questionMapper;

    @Override
    public List<QuestionEntity> queryList ( QuestionEntity questionEntity ) {
        //QueryWrapper<QuestionEntity> queryWrapper = new QueryWrapper<> ();
        if ( questionEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
//        /*if ( questionEntity.getId () != null ) {
//            queryWrapper.eq ( "id", questionEntity.getId () );
//        }
//        if ( StringUtils.isNotBlank ( questionEntity.getTitle () ) ) {
//            queryWrapper.eq ( "title", questionEntity.getTitle () );
//        }
//        if ( StringUtils.isNotBlank ( questionEntity.getContent () ) ) {
//            queryWrapper.eq ( "content", questionEntity.getContent () );
//        }
//        if ( questionEntity.getCategoryId () != null ) {
//            queryWrapper.eq ( "categoryId", questionEntity.getCategoryId () );
//        */}
        List<QuestionEntity> questionEntities = questionMapper.queryList ( questionEntity.getTitle () );
        return questionEntities;
    }


    /**
     * 保存问题或文章，副标题为空则取主标题
     * @param questionEntity
     * @return
     */
    @Override
    public QuestionEntity addSingle (QuestionEntity questionEntity) {
        if ( questionEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        if ( questionEntity.getSubTitle () == null || "".equals ( questionEntity.getSubTitle () )) {
            questionEntity.setSubTitle ( questionEntity.getTitle () );
        }
        questionEntity.setCreateTime ( new Date () );
        int insert = baseMapper.insert ( questionEntity );
        if ( insert == 0 ){
            throw new StateException ( StateEnum.INSERT_EXCE );
        }
        return questionEntity;
    }

    /**
     * 根据问题或文章Id查询信息
     * @param id
     * @return
     */
    @Override
    public QuestionEntity querySingle (int id) {
        if ( id == 0 ) {
            throw new StateException ( StateEnum.PRIMARY_NULL_EXCE );
        }
        QuestionEntity questionEntity = baseMapper.selectById ( id );
        return questionEntity;
    }
}
